package kaizen;

// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 07/02/2013 05:30:06 p.m.
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   serv_permisos.java

import java.io.*;
import java.util.Properties;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import kaizen.jdbc.DbAppUnivC;
import kaizen.jdbc.dbRecord;
import kaizen.nomweb.templates;
import kaizen.nomweb.utilerias;

public class serv_permisos extends HttpServlet
{

    public serv_permisos()
    {
    }

    public void borraCache(String s)
        throws IOException
    {
        String s1 = "/kaizen/" + s + "Web.properties";
        Properties properties = utilerias.loadProperties(s1);
        System.out.println(s1);
        String s2 = properties.getProperty("host");
        System.out.println("host " + s2);
        String s3 = templates.generaPlantilla("/clases/servlet/servlet/ConectAc?dic_estado=54&dic_sistema=" + s, s2);
        System.out.println(s3);
    }

    public Vector cargaArreglo(String s, int i)
        throws IOException
    {
        Vector vector = new Vector();
        Vector vector1 = DbAppUnivC.execQuery(s);
        vector = generaVectorStrings(vector1, i);
        return vector;
    }

    public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        httpservletresponse.setContentType("text/html");
        PrintWriter printwriter = new PrintWriter(httpservletresponse.getOutputStream());
        String s = "";
        HttpSession httpsession = httpservletrequest.getSession(true);
        if(httpsession.getAttribute("usuario") == null)
            return;
        if(httpsession.getAttribute("dic_sistema") != null)
            s = httpsession.getAttribute("dic_sistema").toString();
        else
            s = "nomina";
        String s1 = httpservletrequest.getParameter("opcion");
        System.out.println("La opcion es " + s1);
        if(s1 != null)
        {
            Vector vector = new Vector();
            if(s1.startsWith("gentme"))
            {
                String s2 = "select  me_cveopc, me_numopc, me_cveopc_ejec from gentme where me_cvesis = '" + s + "'";
                vector = cargaArreglo(s2, 3);
            }
            if(s1.startsWith("gentmo"))
            {
                String s3 = "select  mo_cveopc, mo_cveidi, mo_nombre from gentmo where mo_cvesis = '" + s + "'";
                vector = cargaArreglo(s3, 3);
            }
            if(s1.startsWith("gentop"))
            {
                String s4 = "select  op_cveopc, op_tipo, op_url, op_prog_apart from gentop where op_cvesis = '" + s + "'";
                vector = cargaArreglo(s4, 4);
            }
            if(s1.startsWith("gentpe"))
            {
                String s5 = httpservletrequest.getParameter("grupo");
                String s7 = "select pe_cveopc, pe_permisos from gentpe where pe_cvesis = '" + s + "' and pe_gpousu =  '" + s5 + "'";
                vector = cargaArreglo(s7, 3);
            }
            if(s1.startsWith("gentgu"))
            {
                String s6 = "select  gu_grupo, gu_nombre  from gentgu  where gu_cvesis_ini = '" + s + "'";
                vector = cargaArreglo(s6, 2);
            }
            mandaVector(httpservletresponse, vector);
            System.out.println(s1 + " Mandado con exito...");
        } else
        {
            printwriter.println("Error!!!");
        }
        printwriter.close();
    }

    public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        String s = "";
        HttpSession httpsession = httpservletrequest.getSession(true);
        if(httpsession.getAttribute("usuario") == null)
            return;
        if(httpsession.getAttribute("dic_sistema") != null)
            s = httpsession.getAttribute("dic_sistema").toString();
        else
            s = "nomina";
        String s1 = httpservletrequest.getParameter("opcion");
        System.out.println("POST y la opcion es " + s1);
        if(s1.equals("Salvar_Gentpe"))
        {
            Vector vector = recibeVector(httpservletresponse, httpservletrequest);
            String s2 = httpservletrequest.getParameter("grupo");
            guarda_Gentpe(vector, s, s2);
            borraCache(s);
        }
    }

    public Vector generaVectorStrings(Vector vector, int i)
    {
        Vector vector1 = new Vector();
        for(int j = 0; j < vector.size(); j++)
        {
            String as[] = new String[i];
            dbRecord dbrecord = (dbRecord)vector.elementAt(j);
            for(int k = 0; k < i; k++)
            {
                String s = dbrecord.getField(k + 1);
                if(s == null)
                    s = "";
                as[k] = s;
            }

            vector1.addElement(as);
        }

        return vector1;
    }

    public void guarda_Gentme(Vector vector, String s)
        throws IOException
    {
        for(int i = 0; i < vector.size(); i++)
        {
            String as[] = (String[])vector.elementAt(i);
            String s1 = s;
            String s2 = as[0];
            String s3 = as[1];
            String s4 = s;
            String s5 = as[2];
            String s6 = "Insert into gentme(me_cvesis, me_cveopc, me_numopc, me_cvesis_ejec, me_cveopc_ejec)";
            s6 = s6 + " values('" + s1 + "','" + s2 + "'," + s3 + ",'" + s4 + "','" + s5 + "')";
            try
            {
                if(!DbAppUnivC.execStatement(s6))
                    System.out.println("Error SQL: No se pudo realizar el Alta");
            }
            catch(Exception _ex)
            {
                System.out.println("Error SQL: No se pudo realizar el Alta");
            }
        }

    }

    public void guarda_Gentpe(Vector vector, String s, String s1)
        throws IOException
    {
        for(int i = 0; i < vector.size(); i++)
        {
            String as[] = (String[])vector.elementAt(i);
            String s2 = as[0];
            String s3 = as[1];
            String s4 = as[2];
            String s5 = "";
            if(s4.equals("I"))
            {
                s5 = "Insert into gentpe(pe_gpousu, pe_cvesis, pe_cveopc, pe_permisos)";
                s5 = s5 + " values('" + s1 + "','" + s + "','" + s2 + "','" + s3 + "')";
            } else
            if(s3.length() == 0)
            {
                s5 = "delete from  gentpe where pe_gpousu = '" + s1 + "'";
                s5 = s5 + " and  pe_cvesis = '" + s + "' and  pe_cveopc = '" + s2 + "'";
            } else
            {
                s5 = "update gentpe set pe_permisos = '" + s3 + "'";
                s5 = s5 + " where pe_gpousu = '" + s1 + "'";
                s5 = s5 + " and  pe_cvesis = '" + s + "' and  pe_cveopc = '" + s2 + "'";
            }
            try
            {
                if(!DbAppUnivC.execStatement(s5))
                    System.out.println("Error SQL: GentPe");
            }
            catch(Exception _ex)
            {
                System.out.println("Error SQL: GentPe");
            }
        }

    }

    protected void mandaVector(HttpServletResponse httpservletresponse, Vector vector)
    {
        try
        {
            ObjectOutputStream objectoutputstream = new ObjectOutputStream(httpservletresponse.getOutputStream());
            objectoutputstream.writeObject(vector);
            objectoutputstream.flush();
            objectoutputstream.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }
    }

    protected Vector recibeVector(HttpServletResponse httpservletresponse, HttpServletRequest httpservletrequest)
    {
        Object obj = null;
        Object obj1 = null;
        Object obj2 = null;
        Vector vector = new Vector();
        try
        {
            ObjectInputStream objectinputstream = new ObjectInputStream(httpservletrequest.getInputStream());
            vector = (Vector)objectinputstream.readObject();
            objectinputstream.close();
            PrintWriter printwriter = new PrintWriter(httpservletresponse.getOutputStream());
            printwriter.println("Datos enviados con exito");
            printwriter.flush();
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
        return vector;
    }

    public String[] validaArrCommilas(String as[], int i)
    {
        for(int j = 0; j < i; j++)
            as[j] = validaComillas(as[j]);

        return as;
    }

    public String validaComillas(String s)
    {
        if(s == null)
            return "";
        String s1 = s;
        String s2 = "'";
        String s3 = "''";
        for(int i = s1.indexOf(s2); i >= 0;)
        {
            s1 = s.substring(0, i) + s3;
            s1 = s1 + s.substring(i + s2.length(), s.length());
            s = s1;
            int j = s.length();
            int k = i + 2;
            if(k <= j)
                i = s.indexOf(s2, k);
            else
                i = -1;
        }

        return s1;
    }
}